using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace CubeMapToSingleFaces
{
  /// <summary>
  /// This is the main type for your game
  /// </summary>
  public class Game1 : Microsoft.Xna.Framework.Game
  {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public Game1()
    {
      graphics = new GraphicsDeviceManager(this);
      Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
      // TODO: Add your initialization logic here

      base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
      // Create a new SpriteBatch, which can be used to draw textures.
      spriteBatch = new SpriteBatch(GraphicsDevice);
      
      var target_size = 1024;

      var nebula = Content.Load<TextureCube>(@"Backgrounds\nebula");
      var faces = new[]{
          CubeMapFace.PositiveX,
          CubeMapFace.NegativeX,
          CubeMapFace.PositiveY,
          CubeMapFace.NegativeY,
          CubeMapFace.PositiveZ,
          CubeMapFace.NegativeZ,
      };
      foreach (var face in faces)
      {
        var data = new Color[nebula.Size * nebula.Size];
        nebula.GetData(face, data);
        var t = new Texture2D(GraphicsDevice, nebula.Size, nebula.Size);
        t.SetData(data);
        t.SaveAsPng(new System.IO.StreamWriter(@"C:\Users\maggiore\Desktop\CinematicSpaceBattle\Preprocessing\CubeMapToSingleFaces\CubeMapToSingleFaces\bin\x86\Debug\" + face.ToString() + ".png").BaseStream, target_size, target_size);
      }
    }
  }
}
